<?php
namespace Modules\Goods\Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\Goods\Entities\Goods;
use Modules\Goods\Entities\Sku;

class GoodsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 将数据 spider_hm_origin 表导入 goods 表
        Goods::truncate();

        foreach (SpiderHmOrigin::cursor() as $item) {
            if ($item->name == null) continue;

            // for test
            switch ($item->category) {
                case '熟食肠类': $category_id = 47; break;
                case '方便速食': $category_id = 48; break;
                case '糕点': $category_id = 49; break;
                case '调味酱': $category_id = 50; break;
                case '面': $category_id = 51; break;
                case '调味料': $category_id = 52; break;
                case '方便面/粉': $category_id = 53; break;
                case '干果蜜饯': $category_id = 54; break;
            }
            
            $goods = Goods::create([
                'name'  => $item->name,
                'brand'    => $item->brand,
                'thumb'    => $item->image,
                'images'    => $item->images,
                'category_id'    => $category_id,
            ]);

            $goods->skus()->create([
                'sn'    => $item->sn,
                'price'    => intval($item->price * 100),
                'thumb'    => $item->image,
                'specification'    => $item->specification,
            ]);

        }

        $attrs = [
            [
                '颜色' => '金色',
                '规格' => 'M'
            ],
            [
                '颜色' => '金色',
                '规格' => 'XL'
            ],
            [
                '颜色' => '红色',
                '规格' => 'M'
            ],
            [
                '颜色' => '红色',
                '规格' => 'XL'
            ],
        ];

        foreach ($attrs as $index => $value) {
            $sku = Sku::find($index + 10);
            $sku->goods_id = 11;
            $sku->attrabutes = $value;
            $sku->save();
        }
    }
}
